# 打开文件
f = open("D:/Users/李健奇/OneDrive/桌面/Python/基础/文件操作/测试.txt","r",encoding="UTF-8")
# 读取文件  read()
# print(f"文件内容为：{f.read()}")
print("---------------------------------")

# 读取文件 readlines()  读取文件全部行，封装到列表中
# lines = f.readlines()
# print(f"lines对象的类型为：{type(lines)}")
# print(f"lines对象的内容为：{lines}")

# 读取文件 redeline() 读取一行
# line1 = f.readline()
# line2 = f.readline()
# line3 = f.readline()
# print(f"第一行的内容为：{line1}")
# print(f"第二行的内容为：{line2}")
# print(f"第三行的内容为：{line3}")

# for循环读取文件
# for line in f:
#     print(f"每行内容为：{line}")

# 文件的关闭
# f.close()

# with open  自动关闭文件
# with open("D:/Users/李健奇/OneDrive/桌面/Python/基础/文件操作/测试.txt","r",encoding="UTF-8") as f:
#     for line in f:
#         print(f"每行内容为：{line}")


# 综合练习
f = open("D:/Users/李健奇/OneDrive/桌面/Python/基础/文件操作/word.txt","r",encoding="UTF-8")
# 方法1：读取全部内容，通过字符串count方法统计
# line = f.read()
# print(f"文件内容为：{line}")
# num = line.count("itheima")
# print(f"itheima出现的次数为：{num}")
# f.close()

# 方法2
num = 0
for line in f:
    line = line.strip()
    words = line.split(" ")
    print(f"分割后的结果为：{words}")
    for word in words:
        if word == "itheima":
            num += 1
print(f"itheima出现的次数为：{num}")
f.close()